package com.taotao.task;

import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.RedisUtils;
import com.taotao.service.ParamService;
import com.taotao.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 外部接口导入数据到库
 */
@Controller
public class ImportDataTask {
	
	@Autowired
	TaskService taskService;
	@Autowired
	ParamService paramService;
	
	BaseResult baseResult = new BaseResult();

    @RequestMapping("/api/importData")
	@ResponseBody
    public  Map<String, Object>  importData(HttpServletRequest request) {
    	try {
    		boolean lock = RedisUtils.tryGetDistributedLock("lockKey_importData", "importData", 60000);
			if(lock){
				String param = paramService.getParam("job_ImportDataTask");
	    		if("true".equals(param)){
	    			System.out.println("importData执行开始...");
	    			String sourceId = request.getParameter("sourceId");
	    			String page = request.getParameter("page");
	    			taskService.importData(sourceId,page);
	        		System.out.println("importData执行结束...");
	    		}
				RedisUtils.releaseDistributedLock("lockKey_importData", "importData");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
    	return baseResult.getSuccMap();
    }
}